{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "68744723",
   "metadata": {},
   "source": [
    "# Callable\n",
    "在 Python 中，Callable（可调用对象） 指一切可以通过 () 括号语法执行的对象，最常见的就是函数、方法，此外还包括类、lambda 表达式等。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "64f777bd",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "8\n",
      "True\n"
     ]
    }
   ],
   "source": [
    "# 1. 最基础的 Callable：函数（Function）\n",
    "# 1. 定义一个可调用的函数\n",
    "def add(a, b):\n",
    "    return a + b\n",
    "\n",
    "# 2. 调用该函数 使用() \n",
    "result = add(3, 5)  # 执行函数，传入参数\n",
    "print(result)  # 输出：8\n",
    "\n",
    "# 3. 验证是否为 Callable（用 callable() 函数判断）\n",
    "print(callable(add))  # 输出：True（证明 add 是可调用对象）"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "24905d31",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "True\n",
      "24\n",
      "7\n"
     ]
    }
   ],
   "source": [
    "# 2. 匿名的 Callable：lambda 表达式\n",
    "# 1. 定义 lambda 表达式（匿名可调用对象）\n",
    "multiply = lambda x, y: x * y  # 冒号前是参数，冒号后是返回值\n",
    "print(callable(multiply)) \n",
    "# 2. 调用 lambda 表达式\n",
    "result = multiply(4, 6)\n",
    "print(result)  # 输出：24\n",
    "\n",
    "# 3. 直接调用（无需赋值给变量）\n",
    "print((lambda a, b: a - b)(10, 3))  # 输出：7（括号包裹 lambda 后直接加 () 调用）\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "id": "f1b32779",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Alice\n",
      "True\n",
      "False\n"
     ]
    }
   ],
   "source": [
    "# 3. 特殊的 Callable：类（Class）\n",
    "# 1. 定义一个类（类本身是 Callable）\n",
    "class Person:\n",
    "    def __init__(self, name):  # 类的构造方法，调用类时自动执行\n",
    "        self.name = name\n",
    "\n",
    "# 2. 调用类（使用 ()，本质是创建实例）\n",
    "person1 = Person(\"Alice\")  # 调用 Person 类，传入参数 \"Alice\"\n",
    "print(person1.name)  # 输出：Alice（成功创建实例并访问属性）\n",
    "\n",
    "# 3. 验证类是否为 Callable\n",
    "print(callable(Person))  # 输出：True（类是可调用的）\n",
    "print(callable(person1))  # 输出：False（类的实例默认不可调用，除非重写 __call__ 方法）"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "422b8e10",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Alice\n",
      "True\n",
      "True\n",
      "你好, 我是 Alice！\n",
      "Hello, 我是 Alice！\n"
     ]
    }
   ],
   "source": [
    "# 4. 让实例变 Callable：重写 __call__ 方法\n",
    "# 1. 定义一个类（类本身是 Callable）\n",
    "class Person:\n",
    "    def __init__(self, name):  # 类的构造方法，调用类时自动执行\n",
    "        self.name = name\n",
    "    \n",
    "    # 4. 重写 __call__ 方法，让实例可调用\n",
    "    def __call__(self, greeting):\n",
    "        # 调用实例时执行的逻辑：返回带问候语的自我介绍\n",
    "        return f\"{greeting}, 我是 {self.name}！\"\n",
    "\n",
    "# 2. 调用类（使用 ()，本质是创建实例）\n",
    "person1 = Person(\"Alice\")  # 调用 Person 类，传入参数 \"Alice\", 填充的是name\n",
    "print(person1.name)  # 输出：Alice（成功创建实例并访问属性）\n",
    "\n",
    "# 3. 验证类和实例是否为 Callable\n",
    "print(callable(Person))    # 输出：True（类本身可调用）\n",
    "print(callable(person1))   # 输出：True（实例因 __call__ 变得可调用）\n",
    "\n",
    "# 5. 调用实例（像调用函数一样使用 ()）\n",
    "message1 = person1(\"你好\") # 这里的您好，填充的是__call__的greeting变量\n",
    "message2 = person1(\"Hello\")\n",
    "print(message1)  # 输出：你好, 我是 Alice！\n",
    "print(message2)  # 输出：Hello, 我是 Alice！"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "langchain-course (3.10.12)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.10.12"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
